#pragma once 
#include <string>
#include "../json/json.h"
#include "../utils/json.h"
// #include "../middle/commapi.h"
// #include "../utils/type.h"
#include <functional>
#include <set>

// High Water Marks
#define SND_HWM 17408 // 32*1024 - 15*1024
#define BIND_MSG_FUNC(func, iCmd, sSessionId) std::bind(func, this, iCmd, sSessionId, std::placeholders::_1)
#define BIND_API_CALL(func, pApi, iCmd)  std::bind(func, pApi, iCmd, std::placeholders::_1)

/**
 * @brief 获取网卡对应的IP
*/
int GetIpAddress(const char *eth, std::string& ip);
std::set<std::string> GetAllIfname();


class JsonArraySender
{
private:
    typedef std::function<void(Json::Value&)> MsgFunc;

public:
    explicit JsonArraySender(MsgFunc sender) : SendMsg(sender)
    {}

    ~JsonArraySender();

    inline bool isOverSendBuf(const Json::Value& jTotal, const Json::Value& jSingle)
    {
        int iMessageSize = to_string(jTotal).length() + to_string(jSingle).length();
        return iMessageSize > SND_HWM;
    }

    /**
     * @brief 仅支持形如{"array_key":[jsonarray]}格式的json数据
     * 
     * @param rValue 
     * @param array_key 自定义的arr_key
     * @return int 失败-1，成功0
     */
    int send(Json::Value& rValue, const std::string&& array_key);

    void UnPackingSend(Json::Value& jTotal, std::string tag = "");
    void SendLastPackage(Json::Value& jTotal);
private:
    MsgFunc SendMsg;
    bool finished = true;
};



